#include <bits/stdc++.h>
using namespace std;

int n, m;
const int N = 2e3+5;

int a[N], b[N];

pair<int, int> da[N], db[N];

int ma, mb;

int pack(int a[], pair<int, int> da[]) {
	int m = 0;
	for (int i=0, j; i<n; i=j+1) {
		j = i;
		while (j+1<n && a[j+1] == a[i]) ++j;
		da[m++] = make_pair(a[i], j-i+1);
	}
	return m;
}

bool tryit(int x) {
	static pair<int, int> dc[N];
	static int mc = 0;
	mc = ma;
	for (int i=0; i<mc; ++i)
		dc[i] = da[i];
	for (int i=0; i<mc; ++i) {
		dc[i].first = (dc[i].first + x) % m;
	}
	sort(dc, dc+mc);
	for (int i=0; i<mc; ++i)
		if (dc[i] != db[i])
			return false;
	return true;
}

int main(int argc, char const *argv[])
{
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> n >> m;
	for (int i=0; i<n; ++i)
		cin >> a[i];
	for (int i=0; i<n; ++i)
		cin >> b[i];

	sort(a, a+n);
	sort(b, b+n);

	ma = pack(a, da);
	mb = pack(b, db);

	// for (int i=0; i<ma; ++i) {
	// 	printf("%d %d\n", da[i].first, da[i].second);
	// }
	// for (int i=0; i<mb; ++i) {
	// 	printf("%d %d\n", db[i].first, db[i].second);
	// }

	sort(da, da+ma);
	sort(db, db+mb);
	int ans = 0x7fffffff;
	// printf("%d %d\n", db[1].first, da[0].first);
	for (int i=0; i<mb; ++i) {
	
		if (db[i].second != da[0].second)
			continue;
		int x = (db[i].first - da[0].first + m) % m;
		// printf("%d\n", x);
		// if (x == 2) puts("FUCK");
		if (tryit(x)) {
			ans = min(ans, x);
			// break;
		}
	}
	cout << ans << endl;
	return 0;
}